//
//  GSDKTimerHelper.m
//  GameSDK
//
//  Created by Johnny on 2017/7/31.
//  Copyright © 2017年 Johnny. All rights reserved.
//

#import "GSDKTimerHelper.h"
#import "GSDKCommonMethod.h"

static GSDKTimerHelper *shared = nil;

@interface GSDKTimerHelper ()
@property (nonatomic) int verificationCodeTimerNum;
@property (nonatomic,retain) NSTimer *verificationCodeTimer;
@end

@implementation GSDKTimerHelper
+ (GSDKTimerHelper*)shared {
    static dispatch_once_t pred;
    dispatch_once( &pred, ^{
        if (shared == nil) {
            shared = [[self alloc]init];
        }
    });
    return shared;
}

- (void)setupVerificationCodeTimer {
    _verificationCodeTimerNum = 60;
    [[NSNotificationCenter defaultCenter] postNotificationName:VerificationCodeHearbeat object:@(_verificationCodeTimerNum)];
    _verificationCodeTimer = [NSTimer scheduledTimerWithTimeInterval:1
                                              target:self
                                            selector:@selector(verificationCodeTimerAction)
                                            userInfo:nil
                                             repeats:YES];
}

- (void)verificationCodeTimerAction {
    _verificationCodeTimerNum--;
    if (_verificationCodeTimerNum == 0) {
        [_verificationCodeTimer invalidate];
        [[NSNotificationCenter defaultCenter] postNotificationName:VerificationCodeHearbeat object:@0];
        return;
    }
    
    [[NSNotificationCenter defaultCenter] postNotificationName:VerificationCodeHearbeat object:@(_verificationCodeTimerNum)];
}

- (BOOL)canSetupVerificationCodeTimer {
    return _verificationCodeTimerNum == 0? true : false;
}
@end
